<template>
  <!-- 只读文件 -->
  <readOnlyInfo
    v-if="isDisable"
    ref="applyInfoRef"
    applyName="lklpay"
  ></readOnlyInfo>
  <!-- 盛付通进件模板 -->
  <view v-if="!isDisable">
    <uni-collapse ref="collapse">
      <uni-collapse-item
        :show-animation="false"
        title="主体信息"
        class="coll-title"
        :open="true"
      >
        <view class="content">
          <JeePayForm text="商户类型">
            <uni-data-checkbox
              v-model="applyDetailInfo.merchantType"
              :localdata="merchantTypeList"
              @change="mchTypeInfo"
            ></uni-data-checkbox>
          </JeePayForm>

          <template v-if="applyDetailInfo.merchantType != 1">
            <JeePayForm text="营业执照照片">
              <JeepayUpLoad
                ocrFlag="license"
                :imgUrl="applyDetailInfo.licenseImg"
                @uploadSuccess="uploadSuccess($event, 'licenseImg')"
                @clear="clear('licenseImg')"
              />
            </JeePayForm>

            <JeePayForm
              text="营业执照编号"
              v-model:value="applyDetailInfo.licenseNo"
            />

            <JeePayForm text="营业执照起始有效期">
              <termOfValidity
                :defaultDate="applyDetailInfo.licenseEffectBegin"
                @publicSelect="publicSelect($event, 'licenseEffectBegin')"
                :isEnd="false"
              />
            </JeePayForm>

            <JeePayForm text="营业执照有效期截止">
              <termOfValidity
                :defaultDate="applyDetailInfo.licenseEffectEnd"
                @publicSelect="publicSelect($event, 'licenseEffectEnd')"
                @endHandle="endHandle"
              />
            </JeePayForm>
          </template>

          <JeePayForm
            text="商户名称"
            v-model:value="applyDetailInfo.mchFullName"
						tipText="商户名称不少于7个汉字"
          />

          <JeePayForm text="法人身份证人像面照">
            <JeepayUpLoad
              ocrFlag="idCard"
              :imgUrl="applyDetailInfo.idcard1Img"
              @uploadSuccess="uploadSuccess($event, 'idcard1Img')"
              @clear="clear('idcard1Img')"
            />
          </JeePayForm>

          <JeePayForm
            text="法人身份证姓名"
            v-model:value="applyDetailInfo.idcardName"
          />
          <JeePayForm
            text="法人身份证号"
            v-model:value="applyDetailInfo.idcardNo"
          />

          <JeePayForm text="法人身份证国徽面照片">
            <JeepayUpLoad
              ocrFlag="idCard"
              :imgUrl="applyDetailInfo.idcard2Img"
              @uploadSuccess="uploadSuccess($event, 'idcard2Img')"
              @clear="clear('idcard2Img')"
            />
          </JeePayForm>

          <JeePayForm text="法人身份证起始有效期">
            <termOfValidity
              :defaultDate="applyDetailInfo.idcardEffectBegin"
              @publicSelect="publicSelect($event, 'idcardEffectBegin')"
              :isEnd="false"
            />
          </JeePayForm>

          <JeePayForm text="法人身份证结束有效期">
            <termOfValidity
              :defaultDate="applyDetailInfo.idcardEffectEnd"
              @publicSelect="publicSelect($event, 'idcardEffectEnd')"
            />
          </JeePayForm>
        </view>
      </uni-collapse-item>
      <uni-collapse-item
        :show-animation="false"
        title="经营信息"
        class="coll-title"
      >
        <view class="content">
          <JeePayForm text="选择行业MCC">
            <jeeDataPicker
              :code="codeBack.mccCode"
              :localdata="mcc"
              @change="publicSelect($event, 'mccCode')"
              paramType="splicing"
            />
          </JeePayForm>

          <JeePayForm text="经营内容">
            <uni-data-picker
              :localdata="merBusiArray"
              @change="publicSelect($event, 'merBusiContent')"
              v-slot:default="{ data }"
              >{{ merBusiText ? merBusiText : "请选择" }}</uni-data-picker
            >
          </JeePayForm>

          <JeePayForm
            text="商户联系人姓名"
            v-model:value="applyDetailInfo.contactName"
          />
          <JeePayForm
            text="商户联系人手机号"
            v-model:value="applyDetailInfo.contactPhone"
          />

          <JeePayForm text="商户经营省市县">
            <jeeDataPicker
              :code="codeBack.areaCode"
              :localdata="address"
              @change="publicSelect($event, 'areaCode')"
              paramType="arr"
            />
          </JeePayForm>

          <JeePayForm
            text="商户经营地址"
            v-model:value="applyDetailInfo.address"
          />
          <JeePayForm text="门头照">
            <JeepayUpLoad
              :imgUrl="applyDetailInfo.storeOuterImg"
              @uploadSuccess="uploadSuccess($event, 'storeOuterImg')"
              @clear="clear('storeOuterImg')"
            />
          </JeePayForm>

          <JeePayForm text="店内环境照">
            <JeepayUpLoad
              :imgUrl="applyDetailInfo.storeInnerImg"
              @uploadSuccess="uploadSuccess($event, 'storeInnerImg')"
              @clear="clear('storeInnerImg')"
            />
          </JeePayForm>
        </view>
      </uni-collapse-item>
      <uni-collapse-item
        :show-animation="false"
        title="结算账户"
        class="coll-title"
      >
        <view class="content">
          <JeePayForm text="账户类型">
            <uni-data-checkbox
              v-model="applyDetailInfo.settAccountType"
              :localdata="settlementTypeList"
            ></uni-data-checkbox>
          </JeePayForm>

          <JeePayForm
            text="银行开户名"
            v-model:value="applyDetailInfo.settAccountName"
          />
					
					<JeePayForm text="银行卡照片" :start="false">
					  <JeepayUpLoad
					    ocrFlag="bankCard"
					    :imgUrl="applyDetailInfo.settAccountLicenseImg"
					    @uploadSuccess="uploadSuccess($event, 'settAccountLicenseImg')"
					    @clear="clear('settAccountLicenseImg')"
					  />
					</JeePayForm>

          <JeePayForm
            text="银行账号"
            v-model:value="applyDetailInfo.settAccountNo"
          />

          <!-- <JeePayForm text="结算周期">
            <uni-data-picker
              :localdata="settleArray"
              @change="publicSelect($event, 'settlePeriod')"
              v-slot:default="{ data }"
              >{{ settleText ? settleText : "请选择" }}</uni-data-picker
            >
          </JeePayForm> -->

          <JeePayForm
            text="结算人身份证号"
            :start="false"
            tipText="结算人信息不填写默认为法人信息"
            v-model:value="applyDetailInfo.settAccountIdcardNo"
          />

          <JeePayForm :start="false" text="结算人身份证结束有效期">
            <termOfValidity
              :defaultDate="applyDetailInfo.settAccountIdcardEffectEnd"
              @publicSelect="publicSelect($event, 'settAccountIdcardEffectEnd')"
            />
          </JeePayForm>

         <!-- <JeePayForm text="POS类型">
            <uni-data-picker
              :localdata="posArray"
              @change="publicSelect($event, 'posType')"
              v-slot:default="{ data }"
              >{{ posText ? posText : "请选择" }}</uni-data-picker
            >
          </JeePayForm> -->

          <JeePayForm text="其他类型" :start="false">
            <JeepayUpLoad
              :imgUrl="applyDetailInfo.otherFile"
              @uploadSuccess="uploadSuccess($event, 'otherFile')"
              @clear="clear('otherFile')"
            />
          </JeePayForm>
        </view>
      </uni-collapse-item>

      <!-- 盛付通费率组件 -->
      <defaultRate ref="defaultRateRef" />
    </uni-collapse>
  </view>
</template>

<script setup>
import { ref, reactive, inject } from "vue";
import address from "@/components/applyJson/lklAdress.json"; // 地址
import mcc from "@/components/applyJson/mccLkl.json"; // 行业
import JeepayUpLoad from "@/components/JeepayUpLoad/JeepayUpLoad.vue"; // 图片上传
import defaultRate from "../components/defaultRate.vue"; // 默认费率
import readOnlyInfo from "../components//readOnlyInfo.vue"; // 只读文件
import tool from "@/util/tool.js"; // 工具类函数
import JeePayForm from "@/components/applyComponents/JeePayForm.vue"; // 通用左右结构布局
import termOfValidity from "@/components/applyComponents/termOfValidity.vue"; // 选择证件有效期
import jeeDataPicker from "@/components/applyComponents/dataPicker.vue"; // 通用级联选择
import { filterDcit } from "@/hooks/dict"; //字典
const merchantTypeList = reactive(filterDcit(["A0", "A1", "A2"])); // 商户类型
const settlementTypeList = reactive(filterDcit(["B0", "B1"])); // 结算类型

const channelInfo = inject("channelInfo");

const emit = defineEmits(["applyHandle"]);

// 状态分类: 0-未发起、 1-审核中、 2-进件成功、 3-驳回待修改、 4-待验证、 5-待签约
// 当状态为 1 2 4 5 时要开启只读模式，1 3 不开启
let isDisable = ref(false);
if (!["0", "3", "8"].includes(channelInfo.state)) isDisable.value = true;

// 商户进件详细信息（JSON类型字符串）
let applyDetailInfo = reactive({
  merchantType: 3, // 商户类型 默认为3 代表企业
  settAccountType: "B", // 结算账号类型 默认为B 对公
});

// 用于回显的经营内容的文字
let merBusiText = ref("");
// 经营内容
const merBusiArray = [
  { value: "642", text: "百货、中介、培训、景区门票等" },
  { value: "645", text: "交通运输售票" },
  { value: "646", text: "电气缴费" },
  { value: "647", text: "政府类" },
  { value: "648", text: "便民类" },
  { value: "649", text: "公立医院、公立学校、慈善" },
  { value: "650", text: "宾馆餐饮娱乐类" },
  { value: "651", text: "房产汽车类" },
  { value: "652", text: "批发类" },
  { value: "653", text: "超市加油类" },
  { value: "654", text: "一般类商户" },
  { value: "655", text: "三农商户" },
];

// 结算周期
let settleText = ref("");
const settleArray = [
  { value: "T+1", text: "T+1结算" },
  { value: "T+1+N", text: "T+1普通结算批次" },
  { value: "T+3", text: "收单T+3结算" },
  { value: "D+1+N", text: "D+1普通结算批次" },
  { value: "W_T+9999", text: "喔噻T+9999不结算批次" },
  { value: "D+1", text: "收单D+1结算批次" },
  { value: "D1+24", text: "D1+24结算批次" },
  { value: "D+30", text: "收单D+30结算" },
  { value: "BT_D+1", text: "D+1四方补贴结算批次" },
  { value: "QZT_FULL_D+1", text: "钱账通D+1全额结算批次" },
  { value: "QZT_NET_D+1", text: "钱账通D+1净额结算批次" },
];

// 结算周POS类型期
let posText = ref("");
const posArray = [
  { value: " GENERAL_POS", text: "传统POS" },
  { value: "SUPER_POS", text: "智能POS" },
  { value: "BLUE_WIZARD", text: "蓝精灵" },
  { value: "WECHAT_PAY", text: "专业化扫码" },
  { value: "SQB_SCAN_CODE", text: "收钱吧扫码" },
  { value: "SQB_PAPER_CODE", text: "收钱吧码牌" },
  { value: "SQB_DESK_CODE", text: "收钱吧桌码" },
  { value: "SQB_POS", text: "收钱吧POS" },
  { value: "CLOUD_STORE_NEW", text: "新云小店" },
  { value: "CLOUD_DISTRIBUTION", text: "云分销" },
  { value: "CLOUD_DISTRIBUTION_CB", text: "云分销线上" },
  { value: "CLOUD_STORE_CB", text: "云小店线上" },
  { value: "CLOUD_STORE_BC", text: "云小店线下" },
  { value: "CLOUD_STORE_BC_NOTLKL", text: "云小店非收银机" },
  { value: "HM", text: "惠码" },
  { value: "HM_CB", text: "惠码线上" },
  { value: "HM_BC", text: "惠码线下" },
  { value: "SCAN_CODE_ORDER", text: "扫码点餐" },
  { value: "B2B_CASHIER_DESK", text: "B2B收银台" },
  { value: "B2B_QR_CODE", text: "B2B收款码" },
  { value: "MOBILE_POS", text: "手机POS" },
  { value: "YF_YM", text: "御风云码" },
  { value: "YF_YM_CB", text: "御风云码线上" },
  { value: "TRANSFER_ACCOUNT", text: "大额理财" },
  { value: "SUPER_MPOS", text: "超级收款宝" },
  { value: "W_BOX", text: "收钱宝盒" },
  { value: "SCANNING_GUN_PAY", text: "Q码精灵" },
];

// 通用选择函数
const publicSelect = (e, key) => {
  applyDetailInfo[key] = e.detail.value;
  if (key == "merBusiContent") {
    // 经营内容文字回显
    applyDetailInfo.merBusiContent = e.detail.value[0].value;
    merBusiText.value = merBusiArray.find(
      (item) => item.value == e.detail.value[0].value
    ).text;
  } else if (key == "settlePeriod") {
    // 结算周期文字回显
    applyDetailInfo.settlePeriod = e.detail.value[0].value;
    settleText.value = settleArray.find(
      (item) => item.value == e.detail.value[0].value
    ).text;
  } else if (key == "posType") {
    // POS类型文字回显
    applyDetailInfo.posType = e.detail.value[0].value;
    posText.value = posArray.find(
      (item) => item.value == e.detail.value[0].value
    ).text;
  }
};

// 上传图片成功
const uploadSuccess = (res, name) => {
  applyDetailInfo[name] = res.data;
  if (res.ocrInfo && Object.keys(res.ocrInfo).length < 1) return false;
  // OCR图片识别  idCard-身份证 idcard1Img，idcard2Img  license-营业执照 licenseImg
  if (name == "idcard1Img") {
    applyDetailInfo.idcardName = res.ocrInfo.idcardName; // 法人身份证姓名
    applyDetailInfo.idcardNo = res.ocrInfo.idcardNo; // 法人身份证号
  } else if (name == "idcard2Img") {
    applyDetailInfo.idcardEffectBegin = res.ocrInfo.idcardEffectBegin; // 有效期
    applyDetailInfo.idcardEffectEnd = res.ocrInfo.idcardEffectEnd; // 有效期end
  } else if (name == "licenseImg") {
    applyDetailInfo.licenseEffectBegin = res.ocrInfo.licenseEffectBegin; // 执照有效期
    applyDetailInfo.licenseEffectEnd = res.ocrInfo.licenseEffectEnd; // 有效期end
    applyDetailInfo.mchFullName = res.ocrInfo.mchFullName; // 全称
    applyDetailInfo.licenseNo = res.ocrInfo.licenseNo; // 编号
    applyDetailInfo.address = res.ocrInfo.address;
  } else if (name == "settAccountLicenseImg") {
    applyDetailInfo.settAccountNo = res.ocrInfo.settAccountNo; // 银行卡号
  }
};
// 清除图片  仅前端
const clear = (name, url) => (applyDetailInfo[name] = "");

const defaultRateRef = ref(); // 获取费率组件

// 发起进件  注意后面的formCheck参数，表单校验要用
const launchApply = (isTempData) => {
  // 有费率的进件渠道，要在发起进件之前，获取所选择的费率信息
  applyDetailInfo.paywayFeeList = defaultRateRef.value.getList(isTempData);
  emit("applyHandle", {
    name: "lklpay",
    isTempData,
    applyDetailInfo,
    formCheck: [applyDetailInfo.merchantType],
  });
};

// 只读组件信息
let applyInfoRef = ref(null);

// 行业类型，地理位置回显
let codeBack = reactive({
  areaCode: "", // 商户地理位置回显
  mccCode: "",
});

// 合并信息方法, 职能读取，和信息回显，通过此方法合并
const assignObj = (obj) => {
  Object.assign(applyDetailInfo, obj);

  if (!isDisable.value) {
    codeBack.areaCode = obj.areaCode ? obj.areaCode[2] : obj.areaCode; // mcc码，拼接类型要截取
    obj.mccCode ? (codeBack.mccCode = obj.mccCode.split("_")[1]) : null; // mcc码，拼接类型要截取
    // 各类文字回显  经营内容 结算周期    POS类型
    if (obj.merBusiContent)
      merBusiText.value = merBusiArray.find(
        (item) => item.value == obj.merBusiContent
      ).text;
    if (obj.settlePeriod)
      settleText.value = settleArray.find(
        (item) => item.value == obj.settlePeriod
      ).text;
    if (obj.posType)
      posText.value = posArray.find((item) => item.value == obj.posType).text;
  }

  // 以下用于只读信息页
  obj.mccText = tool.mccBack(obj.mccCode, mcc, "splicing"); // 行业码转文字
  obj.areaText = tool.addressBack(obj.areaCode, address); // 地址码转文字
  obj.settAccountText = obj.settAccountType == "C" ? "对私" : "对公";
  obj.merchantText =
    obj.merchantType == 1
      ? "个人"
      : obj.merchantType == 2
      ? "个体工商户"
      : "企业";

  // 传值给只读组件
  if (applyInfoRef.value) applyInfoRef.value.getApplyInof(obj);
};

defineExpose({ launchApply, assignObj });
</script>

<style scoped lang="scss">
@import "../static/information.scss";
</style>
